package com.woodws.fresh.square.service.impl;

import com.woodws.fresh.square.api.dto.DynamicDto;
import com.woodws.fresh.square.api.dto.GetDynamicListDto;
import com.woodws.fresh.square.api.service.InvitationService;
import com.woodws.fresh.square.dao.DynamicDao;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;


@Service
@DubboService
public class InvitationServiceImpl implements InvitationService {

    @Autowired
    private DynamicDao dynamicDao;

    @Override
    public GetDynamicListDto getAllDynamicList(Integer currentPage, Integer pageSize) {
        List<DynamicDto> allDynamicList = dynamicDao.getAllDynamicList(currentPage, pageSize);
        Integer totalRows = dynamicDao.getAllDynamicListCount();

        GetDynamicListDto getDynamicListDto = new GetDynamicListDto();
        getDynamicListDto.setTotalRows(totalRows);
        getDynamicListDto.setDynamicList(allDynamicList);
        return getDynamicListDto;
    }

    @Override
    public GetDynamicListDto getDynamicListForUserId(Integer currentPage, Integer pageSize, String userId) {

        List<DynamicDto> dynamicListForUserId = dynamicDao.getDynamicListForUserId(userId, currentPage, pageSize);
        Integer totalRows = dynamicDao.getDynamicListForUserIdCount(userId);
        GetDynamicListDto getDynamicListDto = new GetDynamicListDto();
        getDynamicListDto.setTotalRows(totalRows);
        getDynamicListDto.setDynamicList(dynamicListForUserId);
        return getDynamicListDto;
    }

    @Override
    public Integer deleteDynamic(String dynamicId) {
        Integer deleteCount = dynamicDao.deleteDynamic(dynamicId);
        DynamicDto dynamic = dynamicDao.getDynamicById(dynamicId);
        if (StringUtils.isNotEmpty(dynamic.getForwardId())){
            dynamicDao.subForwardCount(dynamic.getForwardId());
        }
        return deleteCount;
    }

    @Override
    public GetDynamicListDto getTopDynamicListForUserId(String userId) {
        List<DynamicDto> topDynamicListForUserId = dynamicDao.getTopDynamicListForUserId(userId);
        GetDynamicListDto getDynamicListDto = new GetDynamicListDto();
        getDynamicListDto.setDynamicList(topDynamicListForUserId);

        return getDynamicListDto;
    }

    @Override
    public Integer publishDynamic(DynamicDto dynamicDto) {
        String dynamicId = UUID.randomUUID().toString();
        dynamicDto.setUserId("1111");
        dynamicDto.setDynamicId(dynamicId);
        dynamicDto.setTopFlag("0");
        dynamicDto.setUpCount("0");
        dynamicDto.setCommentCount("0");
        dynamicDto.setFowardCount("0");
        dynamicDto.setDownCount("0");
        dynamicDto.setDeleteFlag("0");
        dynamicDto.setCreateBy("1111");
        Integer count = dynamicDao.publishDynamic(dynamicDto);

        if (!StringUtils.isEmpty(dynamicDto.getForwardId())){
            dynamicDao.addForwardCount(dynamicDto.getForwardId());
        }
        return count;
    }

    @Override
    public Integer giveALikeDynamic(String dynamicId) {
        return dynamicDao.giveALikeDynamic(dynamicId);
    }

    @Override
    public Integer ungiveALikeDynamic(String dynamicId) {
        return dynamicDao.ungiveALikeDynamic(dynamicId);
    }

    @Override
    public Integer giveADislikeDynamic(String dynamicId) {
        return dynamicDao.giveADislikeDynamic(dynamicId);
    }

    @Override
    public Integer ungiveADislikeDynamic(String dynamicId) {
        return dynamicDao.ungiveADislikeDynamic(dynamicId);
    }
}
